package com.tencent.sr.iris.basic.saas.restapi;

import com.tencent.sr.iris.basic.saas.dependency.CommonConfigDependency;
import com.tencent.sr.iris.basic.service.interfaces.request.CommonConfigDeletRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.CommonConfigInvalidRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.CommonConfigQueryRequest;
import com.tencent.sr.iris.basic.service.interfaces.request.CommonConfigRequest;
import com.tencent.sr.iris.basic.service.interfaces.response.CommonConfigItemResponse;
import com.tencent.sr.iris.basic.service.interfaces.response.CommonConfigResponse;
import com.tencent.sr.rmall.common.primitive.HttpResult;
import com.tencent.sr.rmall.common.request.PaginationRequest;
import com.tencent.sr.rmall.common.response.PaginationResponse;
import com.tencent.sr.rmall.springbootstarter.authtoken.localbean.AppUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@Api("通用配置相关接口")
@RequestMapping("/iris/basic/saas/v1/config")
public class CommonConfigController {
    @Resource
    private CommonConfigDependency commonConfigDependency;
    @Resource
    private AppUser appUser;

    @ApiOperation("新建配置计划，配置项")
    @PostMapping("/save")
    HttpResult<Boolean> save(@Valid @RequestBody CommonConfigRequest request) {
        request.setOperateId(appUser.getUid());
        return commonConfigDependency.save(request);
    }

    @ApiOperation("修改配置计划，配置项")
    @PostMapping("/update")
    HttpResult<Boolean> update(@Valid @RequestBody CommonConfigRequest request) {
        request.setOperateId(appUser.getUid());
        return commonConfigDependency.update(request);
    }

    @ApiOperation("web查询配置计划，配置项")
    @PostMapping("/queryConfigs")
    HttpResult<PaginationResponse<CommonConfigResponse>> queryConfigs(
            @Valid @RequestBody PaginationRequest<CommonConfigQueryRequest> request) {
        request.getQueryCriteria().setSaasId(appUser.getSaasId());
        return HttpResult.succ(commonConfigDependency.queryConfigs(request));
    }

    @ApiOperation("删除配置计划，配置项")
    @PostMapping("/deleteConfig")
    HttpResult<Boolean> deleteConfig(@Valid @RequestBody CommonConfigDeletRequest request) {
        request.setOperateId(appUser.getUid());
        return commonConfigDependency.deleteConfig(request);
    }

    @ApiOperation("点击失效配置计划")
    @PostMapping("/invalidConfig")
    HttpResult<Boolean> invalidConfig(@Valid @RequestBody CommonConfigInvalidRequest request) {
        request.setOperateId(appUser.getUid());
        return commonConfigDependency.invalidConfig(request);
    }

    @ApiOperation("配置项/配置计划详情")
    @PostMapping("queryConfigDetail")
    HttpResult<CommonConfigResponse> queryConfigDetail(@Valid @RequestBody CommonConfigRequest request) {
        return HttpResult.succ(commonConfigDependency.queryConfigDetail(request));
    }

    @ApiOperation("查询配置项下拉")
    @PostMapping("queryConfigItems")
    HttpResult<List<CommonConfigItemResponse>> queryConfigItems() {
        return HttpResult.succ(commonConfigDependency.queryConfigItems());
    }
}
